package cn.com.artemis.biz.steinsggs.web.intercepter;

import cn.com.artemis.biz.steinsggs.web.Config;
import cn.com.artemis.biz.steinsggs.web.Constants;
import cn.com.artemis.biz.steinsggs.web.RedisKey;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * SetVariableInterceptor
 * 设置一些额外的信息拦截器
 *
 * @version V1.0.0
 * @author <a href="chufeng.xu@downjoy.com">徐楚风</a>
 * @since 2017-03-08 16:24
 */
public class SetVariableInterceptor extends HandlerInterceptorAdapter implements Constants, RedisKey {

    @Autowired
    private Config config;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String uri = request.getRequestURI();
        if (uri != null && (
                uri.contains("/css/") ||
                        uri.contains("/img/") ||
                        uri.contains("/resources/") ||
                        uri.contains("/js/") ||
                        uri.contains("/themes/")
        )
                ) {
            return true;
        }

        //添加上下文
        String contextPath = request.getContextPath();
        String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+contextPath;
        String resourcesPath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+contextPath + "/resources";
        request.setAttribute("contextPath", contextPath);
        request.setAttribute("basePath", basePath);
        request.setAttribute("resourcesPath",resourcesPath);
        request.setAttribute("backEndUrl", config.getBackEndUrl());
        return true;
    }
}
